package com.godofgamblers.hotelmanagementsystem.mapper;

import com.godofgamblers.hotelmanagementsystem.dto.TimeAndRoomTypeId;
import com.godofgamblers.hotelmanagementsystem.pojo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author wzx
 * @date 2019/10/12 - 11:05
 * 订单类Mapper
 */
@Repository
public interface OrderMapper {
    //用户点击创建订单往订单表中插入数据
    // 插入：（订单号（随机数），user_id,总价格，订单创建时间，支付方式（0=线上支付 1=线下支付），
    //        订单状态（默认0=未支付 1=已支付 2=已取消 3=已失效），留言信息，留言状态（默认0=未读 1=已读），）
    @Insert("insert into `order`(order_num,user_id,total_price,order_time,pay_way,leave_message_context) values(#{order_num},#{user_id},#{total_price},#{order_time},#{pay_way},#{leave_message_context})")
    public void insertOrder(Order order);

    //点击创建订单，获取到对应的room_type_id，将房间类型库存数量减少1（锁定房间15分钟）
    @Update("update room_type set room_type_stock = #{room_type_stock}-1 where room_type_id = #{room_type_id}")
    public void reduce_stock(Room_type_stock_id room);

    //根据room_type_id查询该类房间的库存
    @Select("select room_type_stock from room_type where room_type_id = #{room_type_id} and flag = 1")
    public Integer findStock(Integer room_type_id);

    //用户支付成功后，通过订单号找到该笔订单将该笔订单的状态改为已支付
    //通过order_num查询order_id
    @Select("select order_id from `order` where order_num = #{order_num} and flag = 1")
    public Integer findOrderByOrderNum(String order_num);

    //支付成功后传入订单编号，将该笔订单的支付号插入，订单状态改变（封装一个对象传入）
    @Update("update `order` set deal_num = #{deal_num},order_state = 1 where order_id = #{order_id} and flag = 1")
    public void insertDelNum(Pay_success paySuccess);

    //若15分钟倒计时结束后还没有支付，则代表该笔订单已失效，修改对应状态(参数：订单号)
    @Update("update `order` set order_state = 3,flag = 0 where order_num = #{order_num}")
    public void updateOrderState(String order_num);

    //若15分钟倒计时结束后还没有支付，则代表该笔订单已失效，修改对应状态(参数：订单号)

    //根据order_id找到对应的所有订单项，，0=未支付 1=未使用，2=已使用
    @Select("select * from order_item where order_id = #{order_id} and flag = 1")
    public List<Order_item> findOrderItemByOrderId(Integer order_id);

    //传入订单号，查询对应的订单状态是多少
    @Select("select order_state from `order` where order_num = #{order_num} and flag = 1")
    public Integer findOrderState(String order_num);

    //根据room_type_id查询房间类型名字
    @Select("select room_type_name from room_type where room_type_id = #{room_type_id} and flag = 1")
    public String findRoomTypeName(Integer room_type_id);

    //传入订单号，将对应的订单状态修改为2，已取消
    @Update("update `order` set order_state = 2,flag = 0 where order_num = #{order_num}")
    public void changeOrderState(String order_num);



    //传入入住时间,退房时间，房间类型，查询到该时间内所有的订单项（判断房间被订出去多少间了）
    @Select("SELECT *\n" +
            "FROM order_item\n" +
            "WHERE  ((DATE_FORMAT(plan_stay_time,'%Y-%m-%d') <= #{plan_stay_time} AND #{plan_stay_time}<DATE_FORMAT(plan_leave_time,'%Y-%m-%d'))\n" +
            "\n" +
            "OR (DATE_FORMAT(plan_stay_time,'%Y-%m-%d') < #{plan_leave_time} AND #{plan_leave_time}<= DATE_FORMAT(plan_leave_time,'%Y-%m-%d')))\n" +
            "\n" +
            "AND flag = 1\n" +
            "\n" +
            "AND room_type_id=#{room_type_id}\n" +
            "\n" +
            "AND (order_item_state = 1 OR order_item_state = 2 or order_item_state = 3)")
    public List<Order_item> findnewStock(TimeAndRoomTypeId timeAndRoomTypeId);


    //传入用户，查询到该用户的钱包余额
    @Select("select user_wallet from user where user_id = #{user_id} and flag = 1 and user_online = 1")
    public BigDecimal findUserWallet(User user);

    @Update("update user set user_wallet = #{user_wallet} where user_id = #{user_id} and flag = 1")
    //传入用户对象，将金额修改为支付后的金额
    public void updateUserWallet(User user);


    //钱包支付成功后传入订单编号，将该笔订单的支付号插入，订单状态改变（封装一个对象传入）
    @Update("update `order` set order_state = 1 where order_num = #{order_num} and flag = 1")
    public void updateStateWallet(String order_num);

    //钱包支付失败，将该订单删除
    @Update("update `order` set flag = 0 where order_num = #{order_num}")
    public void updateOrderFlag(String order_num);





    @Select("SELECT SUM(total_price) AS month_money, DATE_FORMAT(order_time,'%Y-%m') AS month\n" +
            "FROM `order` \n" +
            "WHERE order_time LIKE #{year} \n" +
            "GROUP BY DATE_FORMAT(order_time,'%Y-%m')")
    public List<MoneyOfMonth> getYearDataByYear(String year);

    @Select("SELECT SUM(total_price)\n" +
            "FROM `order`\n" +
            "WHERE order_state=1 AND order_time LIKE #{today}")
    public BigDecimal getTodayMoney(String today);




    /**
     * 通过订单id修改对应订单状态
     * @param order 订单pojo类
     * @return int 受影响行数
     */
    @Update("update `order` set order_state=#{order_state} where order_id=#{order_id}")
    public int updateOrderState11(Order order);


    /**
     * 查询订单状态
     * @param order_id 订单id
     * @return 订单id
     */
    @Select("select order_state from `order` where order_id=#{order_id} and flag=1")
    public int findOrderStateByOrderId(int order_id);

    /**
     * 线下开单添加订单
     * @param order 要插入的订单数据
     * @return int 影响行数
     */
    @Insert("insert into `order`(order_num,user_id,total_price,order_time,pay_way,order_state) values(#{order_num},#{user_id},#{total_price},#{order_time},#{pay_way},#{order_state})")
    public int offlineAddOrder(Order order);

    /**
     * 线下根据订单编号查询订单id
     * @param order_num 订单编号
     * @return  订单id
     */
    @Select("select order_id from `order` where order_num=#{order_num} and flag=1")
    public Integer offlineFindOrderIdByOrderNum(String order_num);

    /**
     * 根据订单项id查询是否以计算积分、用户id及价格
     * @param order_id 订单编号
     * @return Order 结果
     */
    @Select("select credit_count,total_price,user_id from `order` where order_id=#{order_id} and flag=1")
    public Order findCreditCountAndPriceByOrderId(Integer order_id);


    /**
     * 修改积分计算状态
     * @param order_id
     */
    @Update("update `order` set credit_count=1 where order_id=#{order_id}")
   public void updateCreditCountByOrderId(Integer order_id);

    /**
     * 查询订单信息
     * @param order_id
     * @return
     */
    @Select("select order_state,order_num,total_price,order_time,user_id from `order` where order_id = #{order_id} and flag=1")
    public Order findOrderinfoByOrderId(Integer order_id);

}
